<template>
  <div>

    <div>
      <el-tag @click="handleAdd()">新增</el-tag>
      <el-tag type="danger">删除</el-tag>
      <el-tag type="info">导入</el-tag>
      <el-tag type="warning">导出</el-tag>
    </div>
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="日志编号" prop="logId"> </el-table-column>

      <el-table-column label="用户id" prop="uid"> </el-table-column>

      <el-table-column label="用户名" prop="uname"> </el-table-column>

      <el-table-column label="操作时间" width="300">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="ip" prop="ip"> </el-table-column>

      <el-table-column label="动作" prop="action"> </el-table-column>

      <el-table-column align="center">
        <template slot="header">
          <el-input size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block" align="center" style="margin-top: 25px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [
        {
          logId: 1,
          uid: 1,
          uname: "test",
          action: "获取全部用户列表",
          createTime: "2022-03-12 16:46:50",
          ip: "0:0:0:0:0:0:0:1",
        },
      ],
      currentPage: 1,
      pageSize: 5,
      total: 100,
    };
  },
  created: function () {
    //页面初始化
    this.$axios
      .get("logs?page=" + this.currentPage + "&pageSize=" + this.pageSize)
      .then((res) => {
        if (res.data.statusCode == 200) {
          this.tableData = res.data.info;
          this.total = res.data.countInfo;
        }
      })
      .catch((error) => {
        console.log(error);
      });
  },
  methods: {
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm(
        "此操作将永久删除" + row.uname + "的信息, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$axios
            .delete("delUser?uid=" + row.uid)
            .then((res) => {
              if (res.data.statusCode == 200) {
                //更新表单???
                console.log(res.data.msg);
                this.tableData.splice(index, 1);
                this.$message({ message: "删除成功", type: "success" });
              } else {
                this.$message.error("删除失败");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.$axios
        .get("logs?page=" + this.currentPage + "&pageSize=" + this.pageSize)
        .then((res) => {
          if (res.data.statusCode == 200) {
            this.tableData = res.data.info;
            this.total = res.data.countInfo;
          }
        });
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.$axios
        .get("logs?page=" + this.currentPage + "&pageSize=" + this.pageSize)
        .then((res) => {
          if (res.data.statusCode == 200) {
            this.tableData = res.data.info;
            this.total = res.data.countInfo;
          }
        });
    },
  },
};
</script>
<style scoped>
#app {
  line-height: 14px;
}
</style>
